package com.mmlc.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.responsebean.BannerResponseBean;
import com.mmlc.utils.DataUtils;
import com.mmlc.utils.ViewUtils;
import com.mmlc.utils.XUtils;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/8.
 */
public class LinearBannerLayout extends LinearLayout {
    public LinearBannerLayout(Context context) {
        super(context);
    }

    public LinearBannerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LinearBannerLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    DisplayMetrics metrics = getResources().getDisplayMetrics();
    List<BannerResponseBean.AdListBean> datas = new ArrayList<>();

    public void addDatas(List<BannerResponseBean.AdListBean> datas) {
        this.datas.addAll(datas);
        addDataSetChanged(datas);
    }

    public void setDatas(List<BannerResponseBean.AdListBean> datas) {
        if (datas == null) return;
        this.datas.clear();
        this.datas.addAll(datas);

        notifyDataSetChanged();
    }

    private void notifyDataSetChanged() {
        removeAllViews();
        addDataSetChanged(this.datas);
    }

    private void addDataSetChanged(final List<BannerResponseBean.AdListBean> datas) {
        for (int i = 0; i < datas.size(); i++) {
            final View view = LayoutInflater.from(getContext()).inflate(R.layout.item_tab_hot_banner, null);
            view.setTag(R.id.tag_one, i);
            final ImageView iv_image = ViewUtils.getView(view, R.id.iv_buy_image);

            XUtils.image(iv_image, datas.get(i).getImg(), new Callback.CommonCallback<Drawable>() {
                @Override
                public void onSuccess(Drawable result) {
                    FrameLayout.LayoutParams ll_params = new FrameLayout.LayoutParams(metrics.widthPixels, result.getIntrinsicHeight() * metrics.widthPixels / result.getIntrinsicWidth());
                    iv_image.setLayoutParams(ll_params);
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                }

                @Override
                public void onCancelled(CancelledException cex) {
                }

                @Override
                public void onFinished() {
                }
            });

            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    int index = (int) view.getTag(R.id.tag_one);
                    DataUtils.toJump(getContext(), datas.get(index));

//                    if (!ViewUtils.isEmpty(datas.get(index).getType())) {
//                        /**  0跳转网页 4商品详情 5商品列表页(分类) 6商品列表页(品牌) */
//                        Intent intent = null;
//                        if ("0".equals(datas.get(index).getType())) {
//                            intent = new Intent(Intent.ACTION_VIEW, Uri.parse(new WebAddress(datas.get(index).getUrl()).toString()));
//                        intent.setClassName("com.Android.browser", "com.android.browser.BrowserActivity");
//                        } else if ("4".equals(datas.get(index).getType())) {
//                            intent = new Intent(getContext(), ProductInfoActivity.class);
//                            intent.putExtra("name", datas.get(index).getName()).putExtra("goods_id", datas.get(index).getUrl());
//                        } else if ("5".equals(datas.get(index).getType())) {
//                            intent = new Intent(getContext(), BrandInfoActivity.class);
//                            intent.putExtra("name", datas.get(index).getName()).putExtra("cate_id", datas.get(index).getUrl());
//                        } else if ("6".equals(datas.get(index).getType())) {
//                            intent = new Intent(getContext(), BrandInfoActivity.class);
//                            intent.putExtra("name", datas.get(index).getName()).putExtra("brand_id", datas.get(index).getUrl());
//                        }
//                        if (intent != null) {
//                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                            getContext().startActivity(intent);
//                        }
//                    }
                }
            });

            addView(view);
        }
    }
}
